package com.odais.AE_Planner;

import java.util.ArrayList;

import org.andengine.entity.primitive.Rectangle;
import org.andengine.opengl.vbo.VertexBufferObjectManager;

import com.odais.AE_Modifier.FlowerSplit;
import com.odais.AE_Modifier.FlowerSplitAngleCalc;
import com.odais.Reservation.ReservationBox;
import com.odais.Reservation.ReservationEntity;

/**
 *
 * @author Lionel DECELLE
 * @since 8 juin 2012 - 10:50:53
 *
 */
public class TableExpanded
{
// ===========================================================
// Constants
// ===========================================================

// ===========================================================
// Fields
// ===========================================================
	private Table table;
	private SceneContext sceneContext;
	private ArrayList<ReservationEntity> reservationEntityList;
	private Rectangle rectangle;
	private FlowerSplitAngleCalc flowerSplitAngleCalc;
	private FlowerSplit flowerSplit;
	private float pX, pY;
	private boolean toggleVisible;
	private int showedItems;

	private VertexBufferObjectManager vertexBufferObjectManager;

// ===========================================================
// Constructors
// ===========================================================
	public TableExpanded(Table table, SceneContext sceneContext,
			ArrayList<ReservationEntity> reservationEntityList) {
		super();
		this.table = table;
		this.sceneContext = sceneContext;
		this.reservationEntityList = reservationEntityList;
		toggleVisible = false;


		vertexBufferObjectManager = sceneContext.getEngine().getVertexBufferObjectManager();

		flowerSplitAngleCalc = new FlowerSplitAngleCalc();

	}

// ===========================================================
// Getter & Setter
// ===========================================================




// ===========================================================
// Methods for/from SuperClass/Interfaces
// ===========================================================
	public void show()
	{

		if(!toggleVisible)
		{
			if(reservationEntityList.size()!=0)
			{
				showedItems = reservationEntityList.size();
				for(int i = 0; i<showedItems; i++)
				{
					reservationEntityList.get(i).getReservationBox_RightPanel().setVisible(false);
				}

				flowerSplitAngleCalc = new FlowerSplitAngleCalc(this.sceneContext.getEngine().getSurfaceWidth()/2, this.sceneContext.getEngine().getSurfaceHeight()/2, reservationEntityList);

				for(int i=0; i<flowerSplitAngleCalc.getShiftedPositions().size(); i++)
				{
					pX = flowerSplitAngleCalc.getShiftedPositions().get(i).getPosX();
					pY = flowerSplitAngleCalc.getShiftedPositions().get(i).getPosY();
					flowerSplit = new FlowerSplit(sceneContext.getContext(), pX, pY, true);
					reservationEntityList.get(i).getReservationBox_RightPanel().setVisible(true);
					reservationEntityList.get(i).getSprite_RightPanel().registerEntityModifier(flowerSplit.deepCopy());
				}
				toggleVisible = true;
			}
		}
		else
		{
			if(reservationEntityList.size()!=0)
			{
				for(int i=0; i<showedItems; i++)
				{
					pX = flowerSplitAngleCalc.getShiftedPositions().get(i).getPosX();
					pY = flowerSplitAngleCalc.getShiftedPositions().get(i).getPosY();
					flowerSplit = new FlowerSplit(sceneContext.getContext(), -pX, -pY, false);
					reservationEntityList.get(i).getSprite_RightPanel().registerEntityModifier(flowerSplit.deepCopy());
				}
				toggleVisible = false;
			}
		}

	}

	public void kill()
	{
		//this.rectangle.setVisible(false);
		if(flowerSplitAngleCalc.getItems().size()!=0)
		{
			for(int i=0; i<flowerSplitAngleCalc.getItems().size(); i++)
			{
				sceneContext.getScene().detachChild(this.reservationEntityList.get(i).getSprite_RightPanel());
			}
		}
	}


}